package com.lkz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkz.entity.Customer;
import com.lkz.entity.Employee;
import com.lkz.mapper.CustomerMapper;
import com.lkz.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkz.utils.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Resource
    private CustomerMapper customerMapper;
    @Override
    public IPage<Customer> selectList(Page<Customer> page,String empName) {
        return customerMapper.selectList(page,empName);
    }

    @Override
    public IPage<Customer> selectDelList(Page<Customer> page, String empName) {
        return customerMapper.selectDelList(page,empName);
    }

    @Override
    public Map<String, Object> uploadExcl(MultipartFile file) {
        Map<String,Object> ruslt = new HashMap<>();
        try {
            String fileName = file.getOriginalFilename();
            Workbook workbook;
            if(fileName.endsWith("xls")){
                workbook = new HSSFWorkbook(file.getInputStream());
            }else if(fileName.endsWith("xlsx")){
                workbook = new XSSFWorkbook(file.getInputStream());
            } else {
                ruslt.put("code","1");
                ruslt.put("message","文件格式非excl");
                return ruslt;
            }
            //判断第一页不为空
            if(null != workbook.getSheetAt(0)){
                //读取excl第二行，从1开始
                for(int rowNumofSheet = 1;rowNumofSheet <=workbook.getSheetAt(0).getLastRowNum();rowNumofSheet++){
                    if (null != workbook.getSheetAt(0).getRow(rowNumofSheet)) {
                        //定义行，并赋值
                        Row aRow = workbook.getSheetAt(0).getRow(rowNumofSheet);
                        Customer customer = new Customer();
                        System.out.println(aRow.getLastCellNum());
                        for(int cellNumofRow=0;cellNumofRow<aRow.getLastCellNum();cellNumofRow++){
                            //读取rowNumOfSheet值所对应行的数据
                            //获得行的列数
                            Cell xCell = aRow.getCell(cellNumofRow);
                            Object cell_val;
                            if(cellNumofRow == 0){
                                if(xCell != null && !xCell.toString().trim().isEmpty()){
                                    cell_val = xCell.getStringCellValue();
                                    if(cell_val != null){
                                        String temp = (String)cell_val;
                                        customer.setCustomerName(temp);

                                    }
                                }
                            }
                            if(cellNumofRow == 1){
                                if(xCell != null && !xCell.toString().trim().isEmpty()){
                                    cell_val = xCell.getStringCellValue();
                                    if(cell_val != null){
                                        String temp = (String)cell_val;
                                        customer.setSex(temp);
                                    }
                                }
                            }
                            if(cellNumofRow == 2){
                                if(xCell != null && !xCell.toString().trim().isEmpty()){
                                    cell_val = xCell.getStringCellValue();
                                    if(cell_val != null){
                                        String temp = (String)cell_val;
                                        customer.setTelphone(temp);
                                    }
                                }
                            }
                            if(cellNumofRow == 3){
                                if(xCell != null && !xCell.toString().trim().isEmpty()){
                                    cell_val = xCell.getStringCellValue();
                                    if(cell_val != null){
                                        String temp = (String)cell_val;
                                        customer.setCompany(temp);
                                    }
                                }
                            }
                            if(cellNumofRow == 4){
                                if(xCell != null && !xCell.toString().trim().isEmpty()){
                                    cell_val = xCell.getStringCellValue();
                                    if(cell_val != null){
                                        String temp = (String)cell_val;
                                        customer.setAddress(temp);
                                    }
                                }
                            }
                            if(cellNumofRow == 5){
                                if(xCell != null && !xCell.toString().trim().isEmpty()){
                                    cell_val = xCell.getStringCellValue();
                                    if(cell_val != null){
                                        String temp = (String)cell_val;
                                        if("会员客户".equals(temp)){
                                            customer.setIsOrders(1);
                                        } else {
                                            customer.setIsOrders(0);
                                        }
                                    }
                                    Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
                                    customer.setEmpId(employee.getEmpId());
                                    customer.setCreateTime(StringUtils.getNowTime());
                                    customer.setUpdateTime(customer.getCreateTime());
                                    customer.setIsDel(0);
                                    customerMapper.insert(customer);
                                }
                            }
                        }

                    }
                }
                ruslt.put("code","0");
                ruslt.put("message","成功插入数据库！");
            }else {
                ruslt.put("code","1");
                ruslt.put("message","第一页EXCL无数据！");
            }
        }catch (Exception e){
            e.printStackTrace();
            ruslt.put("code","1");
            ruslt.put("message",e.getMessage());
        }
        return ruslt;
    }
    @Override
    public List<Customer> list(){
        return customerMapper.list();
    }

}
